function init_header(text){
    $("header").html(text);

    dataservice.getCurrentUser(function(userid){
        console.log("userid=" + userid);
        if(! userid){
            alert("用户没有登录");
            location.href = "login.html";
        }

        $("#userid").html(userid);
    });
}

function init_menu(text){
    $("#sidebar").html(text);

    $(".nav-link").each(function(i, dom){
        var href = location.href;
        if(href.indexOf($(dom).attr("href")) != -1){
            $(dom).addClass("active");
            $(dom).parentsUntil(".nav-dropdown").parent().addClass("open");
        }
    });

    $.navigation = $('nav > ul.nav');

    $(document).ready(function ($) {

        // Add class .active to current link - AJAX Mode off
        $.navigation.find('a').each(function () {

            var cUrl = String(window.location).split('?')[0];

            if (cUrl.substr(cUrl.length - 1) == '#') {
                cUrl = cUrl.slice(0, -1);
            }

            if ($($(this))[0].href == cUrl) {
                $(this).addClass('active');

                $(this).parents('ul').add(this).each(function () {
                    $(this).parent().addClass('open');
                });
            }
        });

        // Dropdown Menu
        $.navigation.on('click', 'a', function (e) {

            if ($.ajaxLoad) {
                e.preventDefault();
            }

            if ($(this).hasClass('nav-dropdown-toggle')) {
                $(this).parent().toggleClass('open');
                resizeBroadcast();
            }
        });

        function resizeBroadcast() {

            var timesRun = 0;
            var interval = setInterval(function () {
                timesRun += 1;
                if (timesRun === 5) {
                    clearInterval(interval);
                }
                window.dispatchEvent(new Event('resize'));
            }, 62.5);
        }

        /* ---------- Main Menu Open/Close, Min/Full ---------- */
        $('.navbar-toggler').click(function () {

            if ($(this).hasClass('sidebar-toggler')) {
                $('body').toggleClass('sidebar-hidden');
                resizeBroadcast();
            }

            if ($(this).hasClass('aside-menu-toggler')) {
                $('body').toggleClass('aside-menu-hidden');
                resizeBroadcast();
            }

            if ($(this).hasClass('mobile-sidebar-toggler')) {
                $('body').toggleClass('sidebar-mobile-show');
                resizeBroadcast();
            }

        });

        $('.sidebar-close').click(function () {
            $('body').toggleClass('sidebar-opened').parent().toggleClass('sidebar-opened');
        });

        /* ---------- Disable moving to top ---------- */
        $('a[href="#"][data-top!=true]').click(function (e) {
            e.preventDefault();
        });

    });

}

function logout() {
    dataservice.logout();
    location.href = "login.html";
}

var bingEventListener = function(obj, trigger, fun){
    if (obj.addEventListener) {
        obj.addEventListener(trigger, fun, false);
    }else if (obj.attachEvent) {
        obj.attachEvent('on' + trigger, fun);
    }else {
        obj['on' + trigger] = fun;
    }
}

bingEventListener(window, 'message', function(data){
    var messagedata = JSON.parse(data.data);
    if(messagedata.type == 'header'){
        init_header(messagedata.data);
    }else if(messagedata.type == 'menu'){
        init_menu(messagedata.data);
    }
});

$("body").append("<iframe style='display: none' src='header.html'></iframe>");
$("body").append("<iframe style='display: none' src='menu.html'></iframe>");


